﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ProjectTask
{
    class ClassTaskDeal
    {
        private SqlConnection scKengee_IT_MIS;
        public DataTable dtTaskDeal = new DataTable();
        public ComboBox cbTaskState = new ComboBox();
        public ComboBox cbSolutionMode = new ComboBox();

        public ClassTaskDeal(SqlConnection scn)
        {
            scKengee_IT_MIS = scn;
            GetTaskDealItems();
        }

        public void TaskDealNewInitial()
        {
            string sql = @"select * from TaskDeal where 1=2";
            SqlDataAdapter sda = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sda.Fill(dtTaskDeal);
            scKengee_IT_MIS.Close();
            sda.Dispose();
            dtTaskDeal.Rows.Add();
        }

        private void GetTaskDealItems()
        {
            string sql = @"select * from ConstantValue where TableName='TaskDeal' order by FieldName,Weight";
            DataTable dtShopInfoItems = new DataTable();
            SqlDataAdapter sdaShopInfo = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShopInfo.Fill(dtShopInfoItems);
            scKengee_IT_MIS.Close();
            sdaShopInfo.Dispose();

            for (int i = 0; i < dtShopInfoItems.Rows.Count; i++)
            {
                string sFieldName=dtShopInfoItems.Rows[i]["FieldName"].ToString();
                string sValue=dtShopInfoItems.Rows[i]["Value"].ToString();
                switch (sFieldName)
                {
                    case "cbTaskState": cbTaskState.Items.Add(sValue); break;
                    case "cbSolutionMode": cbSolutionMode.Items.Add(sValue); break;
                }
            }
        }

        public bool GetTaskDeal_ID(string TaskDeal_ID)
        //获取门店信息
        {
            bool result = false;
            string sql = @"select * from TaskDeal where ID=" + TaskDeal_ID;
            SqlDataAdapter sdaShop = new SqlDataAdapter(sql, scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            sdaShop.Fill(dtTaskDeal);
            scKengee_IT_MIS.Close();
            sdaShop.Dispose();

            if (dtTaskDeal.Rows.Count == 1)
            {
                result = true;
            }
            return result;
        }

        private int UpdateJobTaskState()
        {
            int result = 0;
            string sID = dtTaskDeal.Rows[0]["tbJobTaskID"].ToString();
            string sTaskState = dtTaskDeal.Rows[0]["cbTaskState"].ToString();
            string sql = @"UPDATE [kengee_it_mis].[dbo].[JobTask]
                               SET [cbTaskState] = '" + sTaskState + @"'
                             WHERE ID='" + sID + "'";
            sql = ProjectCommon.ClassCommon.ToDBC(sql);
            SqlCommand scm = new SqlCommand(sql,scKengee_IT_MIS);
            scKengee_IT_MIS.Open();
            result = scm.ExecuteNonQuery();
            scKengee_IT_MIS.Close();
            scm.Dispose();
            return result;
        }

        public int TaskDealAppend()
        //添加新的门店信息，返回值为影响行数，如果为1则成功，不为1则失败
        {
            int result = ProjectCommon.ClassCommon.ClassAppend(scKengee_IT_MIS, dtTaskDeal, "TaskDeal");
            UpdateJobTaskState();
            return result;
        }

        public int TaskDealModify()
        //门店信息修改，返回值为影响行数，如果为1则成功，不为1则失败
        {
            int result = ProjectCommon.ClassCommon.ClassModify(scKengee_IT_MIS, dtTaskDeal, "TaskDeal");
            UpdateJobTaskState();
            return result;
        }
    }
}
